<?php
<<<<<<< HEAD
namespace app\admin\controller;

use app\common\controller\AdminBase;
use \think\Loader;
use \think\Request;
use \think\Url;

class Role extends AdminBase
{
    /**
     * [index description]
     * @author luffy<luffyzhao@vip.126.com>
     * @dateTime 2016-05-19T11:59:59+0800
     * @return [type] [description]
     */
    public function index()
    {
        $ruleModel = Loader::model('Role');
        $lists     = $ruleModel::paginate(20);

        $this->assign('lists', $lists);
        $this->assign('pages', $lists->render());

        return $this->fetch();
    }

    /**
     * [add description]
     * @author luffy<luffyzhao@vip.126.com>
     * @dateTime 2016-05-19T12:22:21+0800
     */
    public function add()
    {
        $request = Request::instance();
        if ($request->isAjax()) {
            $data      = $request->param();
            $roleModel = Loader::model('Role');

            if (loader::validate('Role')->scene('add')->check($data) === false) {
                return $this->error(loader::validate('Role')->getError());
            }

            if (($id = $roleModel->addRole($data)) !== false) {
                Loader::model('BackstageLog')->record("添加用户组,ID:[{$id}]");

                return $this->success('用户组添加成功', Url::build('admin/role/index'));
            }

            return $this->error(Loader::model('Role')->getError());

        }
        $this->assign('ruleRows', Loader::model('rule')->getMenusByParentId());

        return $this->fetch();
    }

    /**
     * [edit description]
     * @author luffy<luffyzhao@vip.126.com>
     * @dateTime 2016-05-19T12:22:26+0800
     * @param  [type] $id [description]
     * @return [type] [description]
     */
    public function edit($id)
    {
        $roleRow = Loader::model('Role')->find($id);

        if ($roleRow == false) {
            $this->error('没有找到对应的数据');
        }

        $request = Request::instance();
        if ($request->isAjax()) {
            $data = $request->param();

            if (loader::validate('Role')->scene('edit')->check($data) === false) {
                return $this->error(loader::validate('Role')->getError());
            }

            if ($roleRow->editRole($data) !== false) {
                Loader::model('BackstageLog')->record("修改用户组,ID:[{$id}]");

                return $this->success('用户组修改成功', Url::build('admin/role/index'));
            }

            return $this->error($roleRow->getError());
        }

        // 用户组所有权限
        $myRuleRows = array_column(Loader::model('Rule')->getRulesByRoleId($id), 'id');

        $this->assign('ruleRows', Loader::model('Rule')->getAllRule());
        $this->assign('roleRow', $roleRow);
        $this->assign('myRuleRows', $myRuleRows);

        return $this->fetch();
    }

    /**
     * [destroy description]
     * @author luffy<luffyzhao@vip.126.com>
     * @dateTime 2016-05-19T12:22:30+0800
     * @param  [type] $id [description]
     * @return [type] [description]
     */
    public function destroy($id)
    {
        $rouleModel = Loader::model('Role');

        if ($rouleModel->deleteRole($id) === false) {
            return $this->error($rouleModel->getError());
        }
        Loader::model('BackstageLog')->record("删除用户组,ID:[{$id}]");

        return $this->success('用户组删除成功', Url::build('admin/role/index'));
    }
}
=======
// +----------------------------------------------------------------------
// | my
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\admin\my\MyPage;
use app\admin\my\MyConfig;

class Role extends Base
{
   
    public function role()
    {
        $my = new MyPage();
        $my->title('角色列表')
           ->buttonNew("roleEdit")

            ->keyId()
            ->keyText('rolename', '角色名称')
            ->setDb('admin_role')
            ->actionEdit('roleEdit?id=###', '编辑')
            ->actionStatus()
            ->actionEdit('giveAccess?id=###&type=enter', '分配权限');

        if ( request()->isAjax() ) {
            return my_data($my,'AdminRole');
        } else {
            return $my->fetch();
        }

    }
    
   
    public function roleEdit($id='')
    {
       
        if(request()->isPost()){
           $res = model('admin/AdminRole')->editData();
           if ($res>0) $this->success('更新成功','role');
           $this->error('更新失败');

        }else{
            $data=my_edit("admin/AdminRole",$id);
            $my = new MyConfig();
            return $my->title("编辑用户")->keyId()
                    ->keyText('rolename', '角色名称')
                    ->data($data)
                    ->fetch(); 
        }

    }

   
    //分配权限
    public function giveAccess()
    {
        $param = input('param.');
        
        if('giveAccess' == $param['type']&&isset($param['rule'])){
            $doparam = [
                'id' => $param['id'],
                'rule' => $param['rule']
            ];
           
            $res = model('admin/AdminRole')->editAccess($doparam);
           
            if ( $res ) {
                $this->success("成功！", 'role');
            } else {
                $this->error("失败！");
            }
            return false;
        }
        
        if('giveAccess' == $param['type']){
            $nodeStr = model('admin/AdminNode')->getNodeInfo($param['id']);
            return json(['code' => 1, 'data' => $nodeStr, 'msg' => 'success']);
        }
        if('enter' == $param['type']){
            return $this->fetch();
        }
        if('index' == $param['type']){
            $this->success("成功！", 'role');
        }

    }
}
>>>>>>> 725e43831064ac36bfd98c75641450c3c41ed15e
